package cn.lidongyu.basic.exception;

import cn.lidongyu.basic.AjaxResult;
import cn.lidongyu.basic.commonenum.ErrorCode;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(CustomException.class)
    public AjaxResult CustomExceptionHandler(CustomException e){
        e.printStackTrace();
        ErrorCode errorCode = ErrorCode.getByCode(e.getMessage());
        return AjaxResult.me().setMessage(errorCode.getMessage()).setCode(errorCode.getCode()).setSuccess(false);
    }
    @ExceptionHandler(Exception.class)
    public AjaxResult exceptionHandler(Exception e){
        e.printStackTrace();
        return AjaxResult
                .me()
                .setCode(ErrorCode.SYSTEM_ERROR.getCode())
                .setSuccess(false)
                .setMessage(ErrorCode.SYSTEM_ERROR.getMessage());
    }

}
